package automatic.generate.code.data;

import java.lang.reflect.Field;
import java.util.List;

import javax.persistence.Column;

import org.misalen.annotations.Enumeration;
import org.misalen.annotations.ModelComment;
import org.misalen.utils.TextUtil;

import automatic.generate.code.model.EnumData;
import automatic.generate.code.model.FiledData;

public abstract class BulidFiled {

	public abstract List<FiledData> bulidFiledDatas(Field... fields);

	protected final void bulidFiledData(FiledData filedData, Field field) {
		Column column = field.getAnnotation(Column.class);
		if (column != null && column.nullable()) {
			filedData.setNullable(true);
		} else {
			filedData.setNullable(false);
		}
		filedData.setClassSimpleName(field.getName());
		filedData.setFiledName(TextUtil.toUpperCaseFirstOne(field.getName()));
		filedData.setFiledType(field.getType().getSimpleName());
		filedData.setComment(field.getAnnotation(ModelComment.class).value());
	}

	protected final FiledData parsingFiled(Field field) {
		FiledData filedData = new FiledData();
		bulidFiledData(filedData, field);
		return filedData;
	}

	protected final FiledData parsingEnumFiled(Field field) {
		EnumData filedData = new EnumData();
		filedData.setEnumSimpleName(field.getAnnotation(Enumeration.class).value());
		bulidFiledData(filedData, field);
		filedData.setFiledType("Enum");
		return filedData;
	}

	protected boolean isPass(Field field) {
		String type = field.getType().getSimpleName();
		if (type.equals("String") || //
				type.equals("Long") || //
				type.equals("Date") || //
				type.equals("Integer")) {
			return false;
		}
		return true;
	}
}
